package qualomelhor.game.entities;

import static playn.core.PlayN.graphics;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.World;

import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.PlayN;
import playn.core.ResourceCallback;
import qualomelhor.game.MyWorld;
import qualomelhor.game.TesteGame;

public abstract class NewDynamicPhysicsEntity implements PhysicsEntity {
	// for calculating interpolation
	private float prevX, prevY, prevA;
	private Body body;
	final ImageLayer layer;
	float x, y, angle, width, height;
	private int scaleFactorX, scaleFactorY;
	private Image img;

	public NewDynamicPhysicsEntity(MyWorld myWorld, World world, float x, float y, float w, float h, float angle, int sfx, int sfy, Image img) {
		this.x = x;
		this.y = y;
		this.angle = angle;
		this.width = w;
		this.height = h;
		this.scaleFactorX = sfx;
		this.scaleFactorY = sfy;
		this.img = img;
		layer = graphics().createImageLayer(img);
		initPreLoad(myWorld);
		TesteGame.setMessageBox("imagem carregada em NewDinamicEntity...w,h = " + img.width() + "," + img.height());
		TesteGame.setMessageBox("getWidth, Height em NewDinamicEntity...w,h = " + getWidth() + "," + getHeight());
		// since the image is loaded, we can use its width and height
		layer.setWidth(img.width());
		layer.setHeight(img.height());
		layer.setOrigin(0, 0);
		//layer.setOrigin(img.width() / 2f, img.height() / 2f);
		
		layer.setScale(scaleFactorX * (getWidth() / img.width()), scaleFactorY * (getHeight() / img.height()));
		//layer.setScale(this.width / img.width(), this.height / img.height());
		layer.setTranslation(x, y);
		layer.setRotation(angle);
		initPostLoad(myWorld);

		body = initPhysicsBody(world, x, y, angle);
		setPos(x, y);
		setAngle(angle);
	}

	public abstract Body initPhysicsBody(World world, float x, float y, float angle);

	//public abstract float getWidth();

	//public abstract float getHeight();
	public float getWidth() {
		return this.width;
	}

	public float getHeight() {
		return this.height;
	}

	public void paint(float alpha) {
		// interpolate based on previous state
		float x = (body.getPosition().x * alpha) + (prevX * (1f - alpha));
		float y = (body.getPosition().y * alpha) + (prevY * (1f - alpha));
		float a = (body.getAngle() * alpha) + (prevA * (1f - alpha));
		//layer.setTranslation(x, y);
		layer.setTranslation(x + (float)((scaleFactorX == -1)?this.width * Math.cos(a):0), y + (float)(this.height * Math.sin(a)));
		//layer.setTranslation(x + getWidth() / 2, y + + getHeight() / 2);
		layer.setRotation(a);
		//layer.setRotation(a + (float)((scaleFactorX == -1)?Math.PI:0f));
		layer.setScale(scaleFactorX * layer.transform().scaleX(), scaleFactorY * layer.transform().scaleY());
	}

	public void update(float delta) {
		// store state for interpolation in paint()
		prevX = body.getPosition().x;
		prevY = body.getPosition().y;
		prevA = body.getAngle();
	}

	public void initPreLoad(final MyWorld myWorld) {
		// attach our layer to the dynamic layer
		myWorld.dynamicLayer.add(layer);
	}

	public void initPostLoad(final MyWorld myWorld) {
	}

	public void setLinearVelocity(float x, float y) {
		body.setLinearVelocity(new Vec2(x, y));
	}

	public void setAngularVelocity(float w) {
		body.setAngularVelocity(w);
	}

	public void setPos(float x, float y) {
		this.x = x;
		this.y = y;
		getBody().setTransform(new Vec2(x, y), getBody().getAngle());
		prevX = x;
		prevY = y;
	}

	public void setAngle(float a) {
		this.angle = a;
		getBody().setTransform(getBody().getPosition(), a);
		prevA = a;
	}

	public Body getBody() {
		return body;
	}

	public void setBody (Body body) {
		this.body = body;
	}

	public ImageLayer getImageLayer() {
		return this.layer;
	}
}